<template>
  <div class="user-content">
    <div class="header">
      <h3 class="title">{{ contentConfig?.tableName ?? '数据列表' }}</h3>
      <el-button
        v-if="contentConfig?.isShowCreateBtn"
        icon="Plus"
        size="large"
        type="primary"
        @click="handeCreateClick"
        >{{ contentConfig?.createBtnText ?? '新建数据' }}</el-button
      >
    </div>
    <div class="user-table">
      <el-table :data="pageList" border style="width: 100%">
        <template v-for="(item, index) in contentConfig.tableList" :key="index">
          <template v-if="item.type === 'slot'">
            <!-- v-bind:将item所有属性绑定到column上 -->
            <el-table-column v-bind="item">
              <template #default="scope">
                <!-- scope中包含row, $index, column, store等属性，一起传给插槽，也可以通过属性绑定传递更多的属性给插槽，如subName="xpp"，在使用插槽的地方可以通过scope.subName获取到xpp -->
                <slot :name="item.slotName" v-bind="scope"></slot>
              </template>
            </el-table-column>
          </template>
          <template v-else>
            <el-table-column v-bind="item" />
          </template>
        </template>
      </el-table>
    </div>

    <div class="user-pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageTotalCount"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 新增用户的对话框 -->
    <PageDialog
      ref="pageDialogRef"
      :centerDialogVisible="isShowCreateDialog"
      :dialogConfig="dialogConfig"
      :isCreate="isCreate"
      @cancel="closeDialog"
    />
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { storeToRefs } from 'pinia'
import useMainStore from '@/store/main/main'
import { formatTime } from '@/utils/format'
import { dialogConfig } from '../config/dialog.config'

import PageDialog from '@/views/main/components/page-dialog.vue'
const currentPage = ref(1)
const pageSize = ref(10)
const isCreate = ref(true) // 区分创建和编辑
const mainStore = useMainStore()

// const pageList = mainStore.pageList // 这边是同步，先执行，正常情况下，异步还没走完，取不到值
const { pageList, pageTotalCount } = storeToRefs(mainStore) // 变成响应式数据，从无到有，防止拿不到数据
const pageDialogRef = ref<InstanceType<typeof PageDialog>>()

interface Iprops {
  contentConfig: {
    pageName: string
    tableName: string
    isShowCreateBtn: boolean
    createBtnText: string
    tableList: any[]
  }
}
const props = defineProps<Iprops>()

getPageList() // 发起网络请求，获取table数据

function handleSizeChange() {
  getPageList()
}

function handleCurrentChange() {
  getPageList()
}

// 发起网络请求，获取列表数据
async function getPageList(dataForm: any = {}) {
  const size = pageSize.value
  const offset = (currentPage.value - 1) * size // 偏移量参数，第一页偏移0条数据，第二页偏移每页展示页码数
  const pageInfo = { size, offset, ...dataForm }
  await mainStore.postPageListAction(props.contentConfig.pageName, pageInfo) // 异步请求,包含搜索信息和页码信息   这边的第一个参数怎么传

  // 请求完成之后，将参数置为默认值
  currentPage.value = 1
  pageSize.value = 10
}

// 新建用户
const isShowCreateDialog = ref(false)
function handeCreateClick() {
  isShowCreateDialog.value = true
  isCreate.value = true
  pageDialogRef.value.resetForm(pageDialogRef.value.dialogForm)
}

function closeDialog() {
  isShowCreateDialog.value = false
  // 要重置表单
  pageDialogRef.value.formRef.resetFields()
}

function setDialogData(val: any) {
  // pageDialogRef.value.dialogForm = val // 不能直接改，直接替换对象会断开响应式链接
  pageDialogRef.value.resetForm(val)
}

defineExpose({
  getPageList,
  isShowCreateDialog,
  isCreate,
  setDialogData,
})
</script>

<style scoped lang="less">
.user-content {
  padding: 20px;
  margin-top: 20px;

  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;

    .title {
      font-size: 24px;
    }
  }

  .user-table {
    :deep(.el-table__cell) {
      padding: 12px 0;
    }
  }
  .user-pagination {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
  }
}
</style>
